<template>
  <div class='top-category'>
    <div class="container">
      <!-- start -->
      <!-- 面包屑 -->
      <XtxBread separator="/">
        <XtxBreadItem to="/">首页</XtxBreadItem>
        <XtxBreadItem>{{cateName}}</XtxBreadItem>
      </XtxBread>

      <!-- 轮播图 -->
      <XtxSlider :data="sliders"/>

      <!-- 二级分类 -->
      <div class="sub-list">
        <h3>全部分类</h3>
        <ul>
          <li v-for="second in categoryList" :key="second.id">
            <a href="javascript:;">
              <img :src="second.picture" >
              <p>{{second.name}}</p>
            </a>
          </li>
        </ul>
      </div>

      <!-- 全部商品 -->
      <!-- 分类关联商品 -->
      <div class="ref-goods" v-for="second in categoryList" :key="second.id">
          <div class="head">
            <h3>-  {{ second.name }}  -</h3>
            <XtxMore to="/" />
          </div>
          <div class="body">
            <SecondGood :key="good.id" v-for="good in second.goods" :good="good"/>
          </div>
      </div>
      <!-- end -->
    </div>
  </div>
</template>

<script>
import { useBanner } from '@/hook/page'
import { findAllCategory } from '@/api/category'
import { onMounted, ref } from 'vue'
import { useRoute } from 'vue-router'
import SecondGood from './components/second-food.vue'
const useCategory = () => {
  // 目录数据
  const categoryList = ref([])

  // 路由实例
  const route = useRoute()

  // 一级目录名称
  const cateName = ref('')

  // 调用接口方法
  const getCategoryList = async (id) => {
    const res = await findAllCategory(id)
    // console.log(res)
    // 赋值
    categoryList.value = res.result.children
    cateName.value = res.result.name
  }

  // 获取数据
  // 传递参数
  onMounted(() => {
    getCategoryList(route.params.id)
  })

  // 监听路由参数变化
  // onBeforeRouteUpdate((to) => {
  //   getCategoryList(route.params.id)
  // })

  // 暴露数据
  return { categoryList, cateName }
}
export default {
  name: 'TopCategory',
  components: {
    SecondGood
  },
  setup () {
    const { sliders } = useBanner()

    // 获取目录数据
    const { categoryList, cateName } = useCategory()

    // 返回数据给模板使用
    return { sliders, categoryList, cateName }
  }
}
</script>

<style scoped lang="less">
.top-category {
  h3 {
    font-size: 28px;
    color: #666;
    font-weight: normal;
    text-align: center;
    line-height: 100px;
  }
  .sub-list {
    margin-top: 20px;
    background-color: #fff;
    ul {
      display: flex;
      padding: 0 32px;
      flex-wrap: wrap;
      li {
        width: 168px;
        height: 160px;
        a {
          text-align: center;
          display: block;
          font-size: 16px;
          img {
            width: 100px;
            height: 100px;
          }
          p {
            line-height: 40px;
          }
          &:hover {
            color: @xtxColor;
          }
        }
      }
    }
  }
  .ref-goods {
    background-color: #fff;
    margin-top: 20px;
    position: relative;
    .head {
      .xtx-more {
        position: absolute;
        top: 20px;
        right: 20px;
      }
      .tag {
        text-align: center;
        color: #999;
        font-size: 20px;
        position: relative;
        top: -20px;
      }
    }
    .body {
      display: flex;
      justify-content: space-around;
      padding: 0 40px 30px;
    }
  }
}
</style>
